<?php

namespace app\serversetting\service\setting;
use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-21 14:13:21
 * @Description: 全局设置
 */

class Setting extends ApiBase
{
	
	protected $settingLogic = 'app\serversetting\logic\Setting';
	protected $settingCategoryLogic = 'app\serversetting\logic\SettingCategory';
    protected $settingValidate = 'app\serversetting\validate\Setting';

    use \app\serversetting\traits\AddCheckAppId;

    //添加配置或编辑某个配置时 进行数据检验 分类的存在性检验
    protected function _checkCateData($data, bool $isEdit): array 
    {
        $scene = $isEdit ? 'edit' : '';
    	$validate = validate($this->settingValidate);
        if(!$validate->scene($scene)->check($data)){
            return verify_err(null, $validate->getError());
        }

        if(isset($data['category_id'])){
            $checkAppId = $this->_addCheckAppId($data, logic($this->settingCategoryLogic), 'dbCateInfo');
            if($checkAppId['status'] != SUCCESS_STATUS){
                return $checkAppId;
            }
        }

        if($isEdit){  //编辑
            $setInfo = logic($this->settingLogic)->getOneSet(['id'=>$data['id'],'app_id'=>$data['app_id']]);
            if($setInfo['status'] != SUCCESS_STATUS){
                return verify_err(null, '该分类不存在');
            }
            $setType = $data['type'] ?? $setInfo['res']['type'];
            $setValue = $data['value'] ?? $setInfo['res']['value'];
            $setOtherInfo = $data['other_info'] ?? $setInfo['res']['other_info'];
            if(in_array($setType, [1,2]) && !empty(array_diff(explode(",", $setValue), array_flip((array)$setOtherInfo)))){
                return verify_err(null, '当类型为radio或checkbox时，value必须在other_info数组中');
            }
            if(isset($data['action'])){
                if($setInfo['res']['action'] == $data['action']){ return auto_success($data, '可操作'); }
            }
        }else{   //添加
            if(in_array($data['type'], [1,2]) && !empty(array_diff(explode(",", $data['value']), array_flip($data['other_info'])))){
                return verify_err(null, '当类型为radio或checkbox时，value必须在other_info数组中');
            }
        }
        if(isset($data['action'])){
            $checkAction = $this->logic($this->settingLogic)->checkAction($data);
            if($checkAction['status'] != SUCCESS_STATUS){
                return $checkAction;
            }
        }
        

        return auto_success($data, '可操作');
    }

    /*添加一个配置*/
    public function addSet(): array 
    {
    	$data = $this->receiveData(['app_id'=>0,'category_id'=>0,'name'=>' ','value'=>'','action'=>' ','type'=>0,'order'=>100,'other_info'=>[]]);

    	$checkData = $this->_checkCateData($data, false);
    	if($checkData['status'] != SUCCESS_STATUS){
    		return $checkData;
    	}
        return $this->logic($this->settingLogic)->addSet($data);
    }

    /*编辑一个配置*/
    public function editSet(): array 
    {
    	$data = $this->issetReceiveData(['app_id'=>0,'id'=>0,'category_id'=>0,'name'=>' ','value'=>' ','action'=>' ','type'=>0,'order'=>100,'other_info'=>[]]);

    	$checkData = $this->_checkCateData($data, true);
    	if($checkData['status'] != SUCCESS_STATUS){
    		return $checkData;
    	}

        return $this->logic($this->settingLogic)->editSet($data);
    }

    /*获取某个具体配置*/
    public function getOneSet(): array 
    {
    	$data = $this->issetReceiveData(['id'=>0, 'action'=>' ', 'app_id'=>0]);
        if(!isset($data['id']) && !isset($data['action'])){
            return verify_err(null, 'id与action两个值必须传一个');
        }
    	return $this->stepRun($data, 'getOneSet');
    }

    /*根据action获取一个或多个配置详情*/
    public function getSomeSet(): array 
    {
        $data = $this->issetReceiveData(['action'=>' ','app_id'=>0]);
        return $this->stepRun($data, 'getSomeSet');
    }

    /*获取某应用配置列表*/
    public function getAppSet(): array 
    {
    	$data = $this->receiveData(['app_id'=>0,'page'=>1,'listRows'=>15]);
    	return $this->stepRun($data, 'getAppSet', 'getAppSet');
    }

    /*获取某些分类下的配置列表*/
    public function getCateSetList(): array 
    {
        $data = $this->haveIssetReceiveData(['action'=>[], 'app_id'=>0]);
        return $this->stepRun($data, 'getCateSetList', 'getCateSetList');
    }

    

    
}






